package cn.glutnn.controller;


import cn.glutnn.entity.Cart;
import cn.glutnn.entity.Result;
import cn.glutnn.service.CartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 购物车表(Cart)表控制层
 *
 * @author msh
 * @since 2024-12-12 16:53:25
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    /**
     * 服务对象
     */
    @Autowired
    private CartService cartService;

    @GetMapping("/listByUserIdAndBusinessId")
    public Result<List<Cart>> listByUserIdAndBusinessId(Long userId,Long businessId) {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, userId);
        wrapper.eq(Cart::getBusinessId, businessId);
        return Result.success(cartService.list(wrapper));
    }

    // 根据用户id查询所有购物车
    @GetMapping("/listByUserId")
    public Result<List<Cart>> listByUserId(Long userId) {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, userId);
        return Result.success(cartService.list(wrapper));
    }

    @PostMapping
    public Result<String> addCart(@RequestBody Cart cart) {
        cartService.save(cart);
        return Result.success("添加成功");
    }

    @PutMapping
    public Result<String> update(@RequestBody Cart cart) {
        cartService.updateById(cart);
        return Result.success("修改成功");
    }

    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Serializable id) {
        cartService.removeById(id);
        return Result.success("删除成功");
    }
}

